package com.chen.authority.service.impl;

import cn.hutool.core.date.DatePattern;
import cn.hutool.core.date.DateTime;
import cn.hutool.core.date.DateUtil;
import cn.hutool.core.map.MapUtil;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.chen.authority.mapper.IndexMapper;
import com.chen.authority.mapper.LoginLogMapper;
import com.chen.authority.mapper.OperationLogMapper;
import com.chen.authority.model.po.LoginLogPo;
import com.chen.authority.model.po.OperationLogPo;
import com.chen.authority.service.IndexService;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

/**
 * 首页图表数据Service业务层处理
 *
 * @author chengy
 * @date 2022-11-19
 */
@Service
@RequiredArgsConstructor
public class IndexServiceImpl implements IndexService {

    private final IndexMapper indexMapper;
    private final LoginLogMapper loginLogMapper;
    private final OperationLogMapper operationLogMapper;

    @Override
    public Map<String, Object> findUserDeptPostCount() {
        return indexMapper.selectUserDeptPostCount();
    }

    @Override
    public List<Map<String, Integer>> findUserSexProportionList() {
        return indexMapper.selectUserSexProportion();
    }

    @Override
    public Map<Object, Object> getLoginAndOperationCycleData() {
        List<String> dateRange = new ArrayList<>();
        List<Long> loginCountList = new ArrayList<>();
        List<Long> operationCountList = new ArrayList<>();

        DateTime lastWeekFirstDay = DateUtil.beginOfDay(DateUtil.lastWeek().toJdkDate());
        dateRange.add(lastWeekFirstDay.toString(DatePattern.NORM_DATE_FORMAT));

        List<LoginLogPo> loginLogs = loginLogMapper.selectList(Wrappers.emptyWrapper());
        List<OperationLogPo> operationLogs = operationLogMapper.selectList(Wrappers.emptyWrapper());

        for (int i = 1; i <= 7; i++) {
            DateTime theNextDay = DateUtil.offsetDay(lastWeekFirstDay, i);
            DateTime theDayBefore = i == 1 ?
                    DateUtil.offsetDay(lastWeekFirstDay, 0)
                    : DateUtil.offsetDay(lastWeekFirstDay, i - 1);

            long loginLogCount = loginLogs.stream()
                    .filter(loginLog -> loginLog.getLoginTime().after(theDayBefore)
                            && loginLog.getLoginTime().before(theNextDay)).count();
            long operationLogCount = operationLogs.stream()
                    .filter(operationLog -> operationLog.getOperTime().after(theDayBefore)
                            && operationLog.getOperTime().before(theNextDay)).count();

            dateRange.add(theNextDay.toString(DatePattern.NORM_DATE_FORMAT));
            loginCountList.add(loginLogCount);
            operationCountList.add(operationLogCount);
        }

        dateRange.remove(dateRange.size() - 1);

        return MapUtil.builder()
                .put("dateRange", dateRange)
                .put("loginCountList", loginCountList)
                .put("operationCountList", operationCountList)
                .map();
    }

    @Override
    public List<Map<String, Integer>> findDepartmentPeopleProportionList() {
        return indexMapper.selectDepartmentPeopleProportion();
    }

    @Override
    public List<Map<String, Integer>> findPostPeopleProportionList() {
        return indexMapper.selectPostPeopleProportion();
    }

}
